<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;

use Illuminate\Http\Request;

class AuthController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Registration & Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users, as well as the
    | authentication of existing users. By default, this controller uses
    | a simple trait to add these behaviors. Why don't you explore it?
    |
    */

    use AuthenticatesAndRegistersUsers, ThrottlesLogins;

    // 验证成功后的重定向地址, 默认为 '/home'
    protected $redirectPath = '/backend/posts';
    // 表单验证成功, 但数据库验证失败时的重定向地址, 默认为 '/auth/login'
    // 表单验证失败时, 直接跳回上级页面
    protected $loginPath = '/backend/auth/login';
    // logout之后的重定向页面, 默认为 '/', 根页面
    protected $redirectAfterLogout = '/backend/auth/login';
    // 需要验证的表单字段, 默认为 'email'
    // 当然还要注意, 表单字段必须和数据库表字段名称相同
    protected $username = 'name';

    /**
     * Create a new authentication controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'getLogout']);
    }

    /**
     * 注册用户时的验证规则
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            // confirmed
            // The field under validation must have a matching field of foo_confirmation.
            // For example, if the field under validation is password,
            // a matching password_confirmation field must be present in the input.
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }

    /**
     * 以下的方法均在 use AuthenticatesAndRegistersUsers, ThrottlesLogins; 这两个trait中给出了
     * 这也是使用Laravel自带的Auth认证的一些默认的实现
     * vendor/laravel/framework/src/Illuminate/Foundation/Auth/AuthenticatesUsers.php
     * 当然, 也可以在该Controller中覆写这些方法以实现手动认证
     */
    /*
       public function getLogin(Request $request)
       {
   //        \Event::fire('access.backend', $request->getClientIp());
           if (view()->exists('auth.authenticate')) {
               return view('auth.authenticate');
           }

           return view('auth.login');
       }

       public function postLogin()
       {

       }

       public function getLogout()
       {

       }

       public function getRegister()
       {

       }

       public function postRegister()
       {

       }
       */
}
